package com.jiuqi.lr_demo.controller;

import com.jiuqi.lr_demo.entity.User;
import com.jiuqi.lr_demo.service.UserService;
import com.jiuqi.lr_demo.utils.Pagination;
import com.jiuqi.lr_demo.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有用户信息
    @RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    public Result<List<User>> getAllUsers(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize) {

        List<User> allUsers = userService.getAllUsers();
        //设置页码范围
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageNum > Pagination.getPages(allUsers.size(), pageSize)) {
            pageNum = Pagination.getPages(allUsers.size(), pageSize);
        }
        //分页
        List<User> paginatedUsers = Pagination.paginateList(allUsers, pageNum, pageSize);
        String total = String.valueOf(allUsers.size());
        log.info("调用了一次分页: " + total);
        return Result.success("查询成功", paginatedUsers, total);
    }

    //根据id查询用户信息
    @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
    public Result<User> getUserById(@PathVariable("id") int id) {
        User user = userService.getUserById(id);
        return Result.success("查询成功", user);
    }


    //Login
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Result<User> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User dbUser = userService.getUser(user);
        return Result.success("登录成功", dbUser);
    }

    //添加用户
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Result<Boolean> addUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean flag = userService.addUser(user);
        return Result.success("添加成功", flag);
    }

    //更新用户
    @RequestMapping(value = "/updateUser/{id}", method = RequestMethod.POST)
    public Result<User> updateUser(@PathVariable("id") Integer id, @RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.getUserById(id);
        user.setUsername(username);
        user.setPassword(password);
        userService.updateUser(user);
        return Result.success("更新成功", user);
    }

    //删除用户
    @RequestMapping(value = "/deleteUser/{id}", method = RequestMethod.DELETE)
    public Result<Boolean> deleteUser(@PathVariable("id") Integer id) {
        boolean flag = userService.deleteUser(id);
        return Result.success("删除成功", flag);
    }





    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public Result<String> test1() {
        return Result.success("data1", "success1");
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public Result<String> test2() {
        System.out.println(1 / 0);
        return Result.success("data2", "success2");
    }
}
